package com.e3.web.index.client;

import java.util.List;

import com.e3.web.core.client.GwtAsyncCallback;
import com.e3.web.index.model.NavigatorModel;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.extjs.gxt.ui.client.widget.menu.SeparatorMenuItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.Element;


public class MainNavigator extends LayoutContainer {
	
	private ToolBar funtionsToolbar;
	
	private ToolBar shortcutsToolbar;
	
	private MenuItemSelectionListener menuSelectionListener = new MenuItemSelectionListener();
	
	private ButtonSelectionListener buttonSelectionListener = new ButtonSelectionListener();
	
	private MainFrameUI mainFrame;
	
	private MainNavigator navigator = this;
	
	public MainNavigator(MainFrameUI mainFrame){
		this.mainFrame = mainFrame;
	}
	
	public MainFrameUI getMainFrame(){
		return this.mainFrame;
	}
	
	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		
		//top panel to show the system menu, which includes funcitons' menu and shortcut button.
		RowLayout hbLayout = new RowLayout(Orientation.HORIZONTAL);
		this.setLayout(hbLayout);
		this.setHeight(30);
		
		//functions' menu
		LayoutContainer leftTBPanel = new LayoutContainer();
		FlowLayout ltbLayout = new FlowLayout(2);
		
		leftTBPanel.setLayout(ltbLayout);
		leftTBPanel.add(getFunctionsToolBar());
		
		//shortcut buttons
		LayoutContainer rightTBPanel = new LayoutContainer();
		FlowLayout rtbLayout = new FlowLayout(2);
		rightTBPanel.setLayout(rtbLayout);
		rightTBPanel.add(getShortcutsToolBar());
				
		this.add(new LayoutContainer(),new RowData(0.2,-1));
		this.add(leftTBPanel,new RowData(0.55,-1));
		this.add(rightTBPanel,new RowData(0.25,-1));
		
		initFunctionsToolBar();
		
		initShortcutsToolBar();
		
	} 
	
	private void initShortcutsToolBar() {
		mainFrame.getService().getAllShortcutModules(new GwtAsyncCallback<List<NavigatorModel>>(this) {
			@Override
			public void doSuccess(List<NavigatorModel> result) {
				if(result == null){
					return;
				}
				for (NavigatorModel md : result) {
					MainButton button = new MainButton(md, buttonSelectionListener);
					getShortcutsToolBar().add(button);
				}
				navigator.layout(true);
			}
		});
	}
	
	private void initFunctionsToolBar() {
		mainFrame.getService().getAllFunctionModules(new GwtAsyncCallback<List<NavigatorModel>>(this) {
			@Override
			public void doSuccess(List<NavigatorModel> result) {
				if(result == null){
					return;
				}
				for (NavigatorModel md : result) {
					createMainButton(getFunctionsToolBar(),md);
				}
				navigator.layout(true);
			}
		});
	}
	
	private MainButton createMainButton(ToolBar toolbar,NavigatorModel md){
		MainButton button = new MainButton(md,null);
		toolbar.add(button);
		Menu menu = createButtonMenu(md);
		if(menu!=null){
			button.setMenu(menu);
		}
		return button;
	}

	private Menu createButtonMenu(NavigatorModel module){
		List<NavigatorModel> subModules = module.getSubModels();
		if(subModules==null||subModules.size()==0){
			return null;
		}

		Menu menu = new Menu();
		menu.setTitle(module.getName());
		for (NavigatorModel md : subModules) {
			if("-".equals(md.getCode())){
				menu.add(new SeparatorMenuItem());
			}
			else{
				MenuItem item = new MainMenuItem(md, menuSelectionListener);
				menu.add(item);
				
				Menu subMenu = createButtonMenu(md);
				if(subMenu!=null){
					item.setSubMenu(subMenu);
				}
			}
			
		}
		return menu;
	}
	


	public ToolBar getFunctionsToolBar(){
		if(funtionsToolbar==null){
			funtionsToolbar = new ToolBar();
		}
		return funtionsToolbar;
	}
	
	public ToolBar getShortcutsToolBar(){
		if(shortcutsToolbar==null){
			shortcutsToolbar = new ToolBar();
		}
		return shortcutsToolbar;
	}
	
	public class MenuItemSelectionListener extends SelectionListener<MenuEvent>{
		@Override
		public void componentSelected(MenuEvent ce) {
			Object item = ce.getItem();
			onElementSelected(item);
		}
		
	}
	
	public class ButtonSelectionListener extends SelectionListener<ButtonEvent>{
		@Override
		public void componentSelected(ButtonEvent ce) {
			Object item = ce.getButton();
			onElementSelected(item);
		}
	}
	
	private void onElementSelected(Object element){
		if(element == null){
			return;
		}
		if(element instanceof MainButton || element instanceof MainMenuItem){
			MainNavigatorListener listener = (MainNavigatorListener)element;
			listener.onElementSelected(getMainFrame());			
		}
	}

}
